package com.dongdongshop.config;

import com.dongdongshop.exception.LoginSellerException;
import com.dongdongshop.pojo.TbSeller;
import com.dongdongshop.service.TbSellerService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

public class SellerRealm extends AuthorizingRealm {

    @DubboReference
    public TbSellerService tbSellerService;

    /*权限管理*/
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }
    /*登录认证*/
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        /*获取录入的账号密码*/
        UsernamePasswordToken token = (UsernamePasswordToken)authenticationToken;
        /*更具录入的账号获取数据库的密码*/
        TbSeller seller = tbSellerService.selectBySellerId(token.getUsername());
        if (seller == null){
            /*用户不存在*/
            return null;
        }
        if (!seller.getStatus().equals("1")){
            throw new LoginSellerException("账号审核未通过或存在异常!");
        }
        /*判断密码*/
        return new SimpleAuthenticationInfo(seller,seller.getPassword(), ByteSource.Util.bytes(seller.getSellerId()),seller.getSellerId());

    }
}
